#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

map <string, pair <int, int> >goods;

int main() {
	int n, k;
	cin >> n >> k;
	string name;
	int shouyi, kucun;

	for (int i = 1; i <= n; i++) {
		cin >> name >> shouyi >> kucun;
		goods[name] = make_pair(shouyi, kucun);
	}

	int i, num, flag = 0;
	long long money = 0;

	for (i = 1; i <= k; i++) {
		cin >> name >> num;
		if (!flag)
			continue;
		pair <int, int> cangku = goods[name];
		if (cangku.second < num) {
			cout << -i;
			flag = 1;
			break;
		} else {
			money += cangku.first * num;
			goods[name] = make_pair (cangku.first, cangku.second - num);
		}
	}
	if (!flag) //flag==0
		cout << money;
	return 0;
}
